﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using HighDiveEngine.Model;
using HighDiveEngine.Resources;

namespace HighDiveAdmin.GUI.Components
{
    /// <summary>
    /// Control that handles editing a dive in the contest history
    /// </summary>
    public partial class RunningContestEditDiveInHistory : UserControl
    {
        /// <summary>
        /// Triggered when the Discard button is clicked
        /// </summary>
        public event MethodInvoker EventDiscardEdit;
        /// <summary>
        /// Triggered when the Save button is clicked
        /// </summary>
        public event DelegateVoidDiveEventArgs EventSaveEdit;

        public RunningContestEditDiveInHistory()
        {
            InitializeComponent();
        }

        #region ButtonClickEvents
        private void buttonDiscard_Click(object sender, EventArgs e)
        {
            if (EventDiscardEdit != null)
                EventDiscardEdit();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (EventSaveEdit != null)
            {
                var scoreArgs = new List<DiveScoreArgs>();

                if (numericUpDownJudge1.Visible)
                    scoreArgs.Add(new DiveScoreArgs { JudgeNumber = 1, Score = Convert.ToDouble(numericUpDownJudge1.Value) });
                if (numericUpDownJudge2.Visible)
                    scoreArgs.Add(new DiveScoreArgs { JudgeNumber = 2, Score = Convert.ToDouble(numericUpDownJudge2.Value) });
                if (numericUpDownJudge3.Visible)
                    scoreArgs.Add(new DiveScoreArgs { JudgeNumber = 3, Score = Convert.ToDouble(numericUpDownJudge3.Value) });
                if (numericUpDownJudge4.Visible)
                    scoreArgs.Add(new DiveScoreArgs { JudgeNumber = 4, Score = Convert.ToDouble(numericUpDownJudge4.Value) });
                if (numericUpDownJudge5.Visible)
                    scoreArgs.Add(new DiveScoreArgs { JudgeNumber = 5, Score = Convert.ToDouble(numericUpDownJudge5.Value) });
                if (numericUpDownJudge6.Visible)
                    scoreArgs.Add(new DiveScoreArgs { JudgeNumber = 6, Score = Convert.ToDouble(numericUpDownJudge6.Value) });
                if (numericUpDownJudge7.Visible)
                    scoreArgs.Add(new DiveScoreArgs { JudgeNumber = 7, Score = Convert.ToDouble(numericUpDownJudge7.Value) });

                EventSaveEdit(new DiveEventArgs
                {
                    DiveCode = textBoxDiveCode.Text,
                    DiveHeight = Convert.ToInt32(textBoxDiveHeight.Text),
                    DiveScores = scoreArgs.ToArray()
                });
            }
        }
        private void numericUpDownJudge1_ValueChanged(object sender, EventArgs e)
        {
            labelTotalScore.Visible = false;
        }
        #endregion

        #region FillFunctions

        /// <summary>
        /// Fills the control with all the information about the dive that's aboutto be edited
        /// </summary>
        /// <param name="dive">Dive to edit</param>
        public void FillDiveToEdit(Dive dive)
        {
            labelContestant.Text = dive.Diver.FirstName + @" " + dive.Diver.LastName;
            labelDiveNumber.Text = dive.DiveNumber.ToString();
            textBoxDiveCode.Text = dive.DiveCode;
            textBoxDiveHeight.Text = dive.DiveHeight.ToString();
            foreach (var judgeScore in groupBoxScore.Controls.OfType<NumericUpDown>())
            {
                judgeScore.Visible = false;
            }
            foreach (var judgeLabel in groupBoxScore.Controls.OfType<Label>())
            {
                judgeLabel.Visible = false;
            }

            foreach (var diveScore in dive.DiveScoreList)
            {
                switch (diveScore.Judge.JudgeNumber)
                {
                    case 1: _setJudgeScore(labelJudge1, numericUpDownJudge1, diveScore.Score); break;
                    case 2: _setJudgeScore(labelJudge2, numericUpDownJudge2, diveScore.Score); break;
                    case 3: _setJudgeScore(labelJudge3, numericUpDownJudge3, diveScore.Score); break;
                    case 4: _setJudgeScore(labelJudge4, numericUpDownJudge4, diveScore.Score); break;
                    case 5: _setJudgeScore(labelJudge5, numericUpDownJudge5, diveScore.Score); break;
                    case 6: _setJudgeScore(labelJudge6, numericUpDownJudge6, diveScore.Score); break;
                    case 7: _setJudgeScore(labelJudge7, numericUpDownJudge7, diveScore.Score); break;
                }
            }
            labelDifficulty.Text = dive.Difficulty.ToString();
            labelDifficulty.Visible = true;
            labelTotalScore.Text = @"Total Score " + Convert.ToDouble(dive.TotalScore).ToString(CultureInfo.InvariantCulture);
            labelTotalScore.Visible = true;
        }

        private void _setJudgeScore(Label judgeLabel, NumericUpDown judgeScore, double score)
        {
            judgeLabel.Visible = judgeScore.Visible = true;
            judgeScore.Value = Convert.ToDecimal(score);
        }
        #endregion

        #region TabStop
        private void RunningContestEditDiveInHistory_TabStopChanged(object sender, EventArgs e)
        {
            foreach (Control control in Controls)
                control.TabStop = TabStop;
        } 
        private void RunningContestEditDiveInHistory_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
                textBoxDiveCode.Focus();
        }
        #endregion

    }
}
